package com.memogame;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.widget.TextView;

public class CreditsActivity extends Activity {

	
	private static String METHOD_NAME = "GetCredits";
	private static String SOAP_ACTION = Data.NAMESPACE + METHOD_NAME;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		TextView text = new TextView(this);
		AsyncCallWS task = new AsyncCallWS(text);
		task.execute();
		//setContentView(R.layout.activity_credits);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_credits, menu);
		return true;
	}
	
	private class AsyncCallWS extends AsyncTask<Void, Void, Void> {

		TextView text;
		String result;

		public AsyncCallWS(TextView text) {
			this.text = text;
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			this.result = getResult();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

			text.setTextSize(18);
			text.setText(this.result);
			text.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

			setContentView(text);
		}

	}
	
	private String getResult(){
		SoapObject Request = new SoapObject(Data.NAMESPACE, METHOD_NAME);

		String result = "Authors: \n";

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(Request);

		try {
			AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
					Data.URL);

			androidHttpTransport.call(SOAP_ACTION, envelope);
			result += envelope.getResponse().toString();			
			Log.d(Data.TAG, "z1 result: " + result);

		} catch (Exception e) {
			Log.e(Data.TAG, "z1 error: " + e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

}
